#!/bin/bash
WAR_OUT_DIR=/home/jenkins/conf/workspace/gitee_shop/platform-framework
TOMCAT_DIR=/home/apache-tomcat-8.5.47
OUTPUT_FILE=$TOMCAT_DIR/logs/catalina.out
WEB_START_CMD=$TOMCAT_DIR/bin/startup.sh
WEB_STOP_CMD=$TOMCAT_DIR/bin/shutdown.sh
TOMCAT_NAME=platform

pid=$(ps aux | grep $TOMCAT_NAME | grep java | awk '{print $2}')
[[ "${pid}x" == "x" ]] && {
        echo "platform-tomcat is not running."
} || {
        $WEB_STOP_CMD
        > $OUTPUT_FILE
        count=15
        while [ $count -gt 0 ];do
                pid=$(ps aux | grep $TOMCAT_NAME | grep java | grep "startup.Bootstrap start" | awk '{print $2}')
                [[ "${pid}x" != "x" ]] && {
                        echo -n .
                        sleep 1
                        count=$((count - 1))
                } || {
                        break
                }
        done
        pid=$(ps aux | grep $TOMCAT_NAME | grep java | awk '{print $2}')
        [[ "${pid}x" != "x" ]] && {
                kill -9 $pid;
        }
        echo
        echo "platform-tomcat be killed successfully."
}
mv $WAR_OUT_DIR/target/platform.war $TOMCAT_DIR/webapps/platform.war
rm -rf $TOMCAT_DIR/webapps/platform
echo "everything is ready"
$WEB_START_CMD
